﻿using System;
using System.Threading.Tasks;

namespace NazTek.Core
{
    /// <summary>
    /// Encapsulates execution mechanism for synchronous and asynchronous support.  
    /// </summary>
    public interface IExecutor
    {
        /// <summary>
        /// Executes an operation.
        /// </summary>
        /// <param name="action">The action to execute.</param>
        void Execute(Action action);
    }
    /// <summary>
    /// Provides synchronous execution implementation.
    /// </summary>
    public sealed class SyncExecutor : IExecutor
    {
        /// <summary>
        /// Executes operation in sync.
        /// </summary>
        /// <param name="action">The action to execute.</param>
        public void Execute(Action action)
        {
            action();
        }
    }
    /// <summary>
    /// Provides asynchronous execution mechanism.
    /// </summary>
    public sealed class AsyncExecutor : IExecutor
    {
        /// <summary>
        /// Executes operation asynchronously.
        /// </summary>
        /// <param name="action">The action to execute.</param>
        public void Execute(Action action)
        {
            Task.Run(action).ContinueWith(t => t.Dispose());
        }
    }
}